'''
Description: 
Version: 2.0
Autor: zyw
Date: 2021-12-22 17:45:43
LastEditors: zyw
LastEditTime: 2022-01-10 17:19:58
'''
import logging
import os, sys

base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(base_path)
from common.hand_path import log_path
from common.hand_conf import conf


def create_log(name='test', level='DEBUG', filename='log.log', control_level='DEBUG', file_level='DEBUG'):
    # 第一步：创建一个日志收集器
    log = logging.getLogger(name)

    # 第二步：设置收集器收集日志的等级
    log.setLevel(level)

    # 第三步：设置输出渠道
    # 3.1、创建一个日志输出的渠道,输出到控制台
    sc = logging.StreamHandler()
    sc.setLevel(control_level)
    log.addHandler(sc)

    # #3.2、创建一个日志输出的渠道,输出到文件
    log_file = logging.FileHandler(filename, encoding='utf-8')
    log_file.setLevel(file_level)
    log.addHandler(log_file)

    # 第四步：设置日志输出的格式
    log_format = logging.Formatter('%(asctime)s-【%(filename)s-->line:%(lineno)d】-%(levelname)s:%(message)s')
    # 设置输出到文件的日志格式
    log_file.setFormatter(log_format)

    # 返回一个日志收集器
    return log


my_log = create_log(
    name=conf.get('logging', 'name'),
    level=conf.get('logging', 'level'),
    filename=os.path.join(log_path, conf.get('logging', 'filename')),
    control_level=conf.get('logging', 'control_level'),
    file_level=conf.get('logging', 'file_level')
)
